from __future__ import division
import math

try:
	from psyco.classes import *
except ImportError:
	pass

def AngleVector(angle, magnitude=1):
	return Vector((magnitude * math.cos(angle), magnitude * math.sin(angle), 0))

class Vector(object):
	def __init__(self, dimensions):
		self.x, self.y, self.z = dimensions
	
	def draw(self):
		#return int(self.x), int(self.y)
		return self.x, self.y
	
	def __getitem__(self, index):
		return (self.x, self.y, self.z)[index]
	
	def __iter__(self):
		return iter((self.x, self.y, self.z))
	
	def copy(self):
		return Vector((self.x, self.y, self.z))
	
	__copy__ = copy
	
	def __eq__(self, vector):
		return (self.x, self.y, self.z) == (vector.x, vector.y, vector.z)
	
	def __add__(self, vector):
		return Vector((self.x + vector.x, self.y + vector.y, self.z + vector.z))
		
	__radd__ = __add__
	
	def __iadd__(self, vector):
		self.x += vector.x
		self.y += vector.y
		self.z += vector.z
		
		return self
	
	def __sub__(self, vector):
		return Vector((self.x - vector.x, self.y - vector.y, self.z - vector.z))
		
	def __rsub__(self, vector):
		return Vector((vector.x - self.x, vector.y - self.y, vector.z - self.z))
	
	def __neg__(self):
		return Vector((-self.x, -self.y, -self.z))
	
	def __isub__(self, vector):
		self.x -= vector.x
		self.y -= vector.y
		self.z -= vector.z
		
		return self
	
	def __mul__(self, scalar):
		return Vector((self.x * scalar, self.y * scalar, self.z * scalar))
		
	__rmul__ = __mul__
		
	def __imul__(self, scalar):
		self.x *= scalar.x
		self.y *= scalar.y
		self.z *= scalar.z
		
		return self
	
	def __truediv__(self, scalar):
		return Vector((self.x / scalar, self.y / scalar, self.z / scalar))
	
	def __itruediv__(self, scalar):
		self.x /= scalar
		self.y /= scalar
		self.z /= scalar
		
		return self
	
	def __div__(self, scalar):
		return Vector((self.x / scalar, self.y / scalar, self.z / scalar))
	
	def __idiv__(self, scalar):
		self.x /= scalar
		self.y /= scalar
		self.z /= scalar
		
		return self
	
	def __str__(self):
		return "(%s, %s, %s)" % (self.x, self.y, self.z)
	
	def __repr__(self):
		return "Vector(%s)" % str(self)
	
	def dot(self, vector):
		return self.x * vector.x + self.y * vector.y + self.z * vector.z
	
	def cross(self, vector):		
		return Vector((self.y*vector.z - self.z*vector.y, 
						self.z*vector.x - self.x*vector.z, 
						self.x*vector.y - self.y*vector.x))
	
	def magnitude(self):
		return math.sqrt(self.x**2 + self.y**2 + self.z**2)
	
	def normalised(self):
		return self / self.magnitude()
	
	@property
	def dimensions(self):
		return (self.x, self.y, self.z)

def dot(a, b):
	return a.x * b.x + a.y * b.y + a.z * b.z
